package cn.com.darly.videoandroidpm.ui.registerfriend;

import android.os.Bundle;
import android.view.View;

import com.darly.common.ToastApp;
import com.darly.dview.framework.ContentBinder;
import com.darly.dview.framework.ViewsBinder;
import com.darly.dview.widget.clearedit.ClearEditText;
import com.darly.dview.widget.header.TitleView;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import cn.com.darly.videoandroidpm.R;
import cn.com.darly.videoandroidpm.base.BaseActivity;
import cn.com.darly.videoandroidpm.bean.FriendInfo;
import cn.com.darly.videoandroidpm.bean.LoginInfo;
import cn.com.darly.videoandroidpm.common.SharePreferHelp;
import cn.com.darly.videoandroidpm.ui.biz.AddFriendListener.AddFriendInterface;
import cn.com.darly.videoandroidpm.ui.presenter.AddFriendPresenter;

/**
 *  添加好友页面，顶部一个搜索框，输入用户手机号。或者用户昵称，进行搜索用户。
 * Copyright (c) 2017 Organization D.L. zhangyuhui All rights reserved.
 * @author  Darly/张宇辉/2018/4/19 10:49
 * @version  1.0/cn.com.darly.videoandroidpm
 */
@ContentBinder(R.layout.activity_apply_friend)
public class ApplyFriendActivity extends BaseActivity implements View.OnClickListener,AddFriendInterface {
    @ViewsBinder(R.id.id_base_title)
    public TitleView id_base_title;

    @ViewsBinder(R.id.id_apply_friend_msg)
    ClearEditText id_apply_friend_msg;
    @ViewsBinder(R.id.id_apply_friend_nickname)
    ClearEditText id_apply_friend_nickname;

    LoginInfo friend;
    AddFriendPresenter presenter;
    @Override
    protected void initView(Bundle savedInstanceState) {
        friend = (LoginInfo) getIntent().getSerializableExtra("LoginInfo");
        id_base_title.setTitle("添加申请");
        presenter = new AddFriendPresenter(this);
    }


    @Override
    protected void loadData() {
        LoginInfo info = (LoginInfo) SharePreferHelp.getValue("LoginInfo");
        if (info!=null){
            id_apply_friend_msg.setText("我是："+info.getName());
        }

        if (friend !=null) {
            id_apply_friend_nickname.setText(friend.getName());
        }
    }

    @Override
    protected void initListener() {
        id_base_title.setLeftBackOneListener(R.mipmap.ic_title_back,this);
        id_base_title.setRightViewRightTextOneListener("申请",this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.title_view_ok:
                Map<String, String> params  = new LinkedHashMap<String, String>();
                params.put("apply",id_apply_friend_msg.getText().toString().trim());
                params.put("nickname",id_apply_friend_nickname.getText().toString().trim());
                if (friend!=null) {
                    params.put("userid", friend.getId());
                }
                presenter.addFriend(params);
                break;
            default:
                presenter.onClickDown(this,v);
                break;
        }

    }

    @Override
    public void onSearchSuccess(List<LoginInfo> friendInfos) {

    }

    @Override
    public void onSearchFailed(String info) {
        ToastApp.showToast(info);
    }

    @Override
    public void onAddFriendSuccess(Object code) {
        //申请好友成功，消息发送到服务端
        setResult(200);
        end();
    }

    @Override
    public void onAddFriendFailed(String info) {
        ToastApp.showToast(info);
    }

    @Override
    public void onApplySuccess(List<FriendInfo> friendInfos) {

    }

    @Override
    public void onApplyFailed(String info) {

    }

    @Override
    public String findFriendName() {
        return null;
    }

    @Override
    public void end() {
        finish();
    }

    @Override
    public void setDisableClick() {

    }

    @Override
    public void setEnableClick() {

    }
}
